package com.zerui.manager.app.deviceTouch;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zerui.cola.dto.PageResponse;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.app.deviceTouch.executer.DeviceTouchQryCmdExe;
import com.zerui.manager.web.DeviceTouchService;
import com.zerui.manager.web.cmd.DeviceInfoCmd;
import com.zerui.manager.web.cmd.DeviceTouchQryCmd;
import com.zerui.manager.web.vo.DevicePageVO;
import com.zerui.manager.web.vo.DeviceTouchPageVO;
import com.zerui.manager.web.vo.feign.DeviceInfoFeignVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceTouchServiceImpl implements DeviceTouchService {

    @Resource
    private DeviceTouchQryCmdExe touchQryCmdExe;

    @Override
    public PageResponse<DeviceTouchPageVO> getPageDeviceList(DeviceTouchQryCmd qryCmd) {
        Page<DeviceTouchPageVO> deviceTouchPageVOPage = touchQryCmdExe.getPageDeviceList(qryCmd);
        return PageResponse.of(deviceTouchPageVOPage.getRecords(), (int) deviceTouchPageVOPage.getTotal(),
                (int) deviceTouchPageVOPage.getSize(),
                (int) deviceTouchPageVOPage.getCurrent());
    }

    @Override
    public PageResponse<DevicePageVO> getPageDeviceList2(DeviceInfoCmd deviceInfoCmd) {
        DeviceTouchQryCmd qryCmd = this.convertParam(deviceInfoCmd);
        Page<DeviceTouchPageVO> deviceTouchPageVOPage = touchQryCmdExe.getPageDeviceList2(qryCmd);
        List<DevicePageVO> devicePageVOList = new ArrayList<>();
        for (DeviceTouchPageVO item : deviceTouchPageVOPage.getRecords()) {
            DevicePageVO deviceItemVO = new DevicePageVO();
            deviceItemVO.setDeviceId(Long.parseLong(item.getDeviceId()));
            deviceItemVO.setName(item.getDeviceName());
            deviceItemVO.setCode(item.getDeviceCode());
            deviceItemVO.setOrgId(null != item.getOrgId() ? Long.parseLong(item.getOrgId()) : null); // 部门ID
            deviceItemVO.setGroupName(item.getGroupName());
            deviceItemVO.setType(item.getTypeCode());
            deviceItemVO.setTypeCode(item.getProductCode());
            deviceItemVO.setTypeName(item.getProductName());
            deviceItemVO.setStatus(item.getIsOnline()); // 在线状态
            deviceItemVO.setNodeType(null != item.getNodeType() ? Integer.parseInt(item.getNodeType()) : null);
            deviceItemVO.setNodeTypeName(item.getNodeTypeName());
            deviceItemVO.setPosition(item.getPosition()); // 位置信息
            deviceItemVO.setAccess(item.getAccess());
            deviceItemVO.setRunStatus(item.getRunStatus());
            deviceItemVO.setCurfewStatus(item.getCurfewStatus());
            deviceItemVO.setEnableStatus(item.getEnableStatus());
            deviceItemVO.setStartTime(item.getStartTime());
            deviceItemVO.setEndTime(item.getEndTime());
            deviceItemVO.setCreateTime(item.getCreateTime());
            devicePageVOList.add(deviceItemVO);
        }
        return PageResponse.of(devicePageVOList, (int) deviceTouchPageVOPage.getTotal(),
                (int) deviceTouchPageVOPage.getSize(),
                (int) deviceTouchPageVOPage.getCurrent());
    }

    @Override
    public DeviceTouchPageVO getDeviceByDeviceCode(String deviceCode) {
        return touchQryCmdExe.getDeviceByDeviceCode(deviceCode);
    }

    @Override
    public SingleResponse<List<DeviceInfoFeignVO>> getAccessControlDeviceList(DeviceInfoCmd deviceInfoCmd) {
        DeviceTouchQryCmd qryCmd = this.convertParam(deviceInfoCmd);
        List<DeviceInfoFeignVO> list = touchQryCmdExe.getAccessControlDeviceList(qryCmd);
        return SingleResponse.of(list);
    }

    private DeviceTouchQryCmd convertParam(DeviceInfoCmd deviceInfoCmd) {
        DeviceTouchQryCmd qryCmd = new DeviceTouchQryCmd();
        qryCmd.setPageIndex(deviceInfoCmd.getPageIndex());
        qryCmd.setPageSize(deviceInfoCmd.getPageSize());
        qryCmd.setDeviceCode(deviceInfoCmd.getCode());
        qryCmd.setProductCode(deviceInfoCmd.getType()); // 产品号
        qryCmd.setIsOnline(deviceInfoCmd.getStatus());
        qryCmd.setDeviceName(deviceInfoCmd.getName());
        qryCmd.setDeviceFrom(deviceInfoCmd.getDeviceFrom()); // 设备源
        qryCmd.setOrgId(null != deviceInfoCmd.getOrgId() ? String.valueOf(deviceInfoCmd.getOrgId()) : null); //
//	    qryCmd.setTypeCode(deviceInfoCmd.getType());
        if (null != deviceInfoCmd.getPositionCmd()) { // 位置信息
            qryCmd.setBuildNo(null != deviceInfoCmd.getPositionCmd().getBuildingId() ? String.valueOf(deviceInfoCmd.getPositionCmd().getBuildingId()) : null);
            qryCmd.setLevelNo(null != deviceInfoCmd.getPositionCmd().getLevelId() ? String.valueOf(deviceInfoCmd.getPositionCmd().getLevelId()) : null);
            qryCmd.setRoomNo(null != deviceInfoCmd.getPositionCmd().getRoomId() ? String.valueOf(deviceInfoCmd.getPositionCmd().getRoomId()) : null);
        }
        return qryCmd;
    }
}
